package com.sofa.demo.client;

import com.alipay.sofa.registry.client.api.RegistryClientConfig;
import com.alipay.sofa.registry.client.api.SubscriberDataObserver;
import com.alipay.sofa.registry.client.api.model.UserData;
import com.alipay.sofa.registry.client.api.registration.SubscriberRegistration;
import com.alipay.sofa.registry.client.provider.DefaultRegistryClient;
import com.alipay.sofa.registry.client.provider.DefaultRegistryClientConfigBuilder;
import com.alipay.sofa.registry.core.model.ScopeEnum;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.config.RegistryConfig;
import com.sofa.demo.service.HelloService;


public class Client {
    public static void main(String[] args) {

//        // 构建客户端实例
//        RegistryClientConfig config = DefaultRegistryClientConfigBuilder.start().setRegistryEndpoint("127.0.0.1").setRegistryEndpointPort(9603).build();
//        DefaultRegistryClient registryClient = new DefaultRegistryClient(config);
//        registryClient.init();
//
//// 创建 SubscriberDataObserver
//        SubscriberDataObserver subscriberDataObserver = new SubscriberDataObserver() {
//            public void handleData(String dataId, UserData userData) {
//                System.out.println("receive data success, dataId: " + dataId + ", data: " + userData);
//            }
//        };
//
//// 构造订阅者注册表，设置订阅维度，ScopeEnum 共有三种级别 zone, dataCenter, global
//        String dataId = "com.alipay.test.demo.service:1.0@DEFAULT";
//        SubscriberRegistration registration = new SubscriberRegistration(dataId, subscriberDataObserver);
//        registration.setScopeEnum(ScopeEnum.global);
//
//// 将注册表注册进客户端并订阅数据，订阅到的数据会以回调的方式通知 SubscriberDataObserver
//        registryClient.register(registration);



        // 1. 注册中心配置
        RegistryConfig registryConfig = new RegistryConfig()
                .setProtocol("sofa") // 设置协议
                .setAddress("127.0.0.1:9603"); // 设置注册中心 session 地址

//                .setProtocol("zookeeper")
//                .setAddress("127.0.0.1:2181")
                ;


        // 2. consumer 综合配置
        ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>()
                .setInterfaceId(HelloService.class.getName()) // 指定接口
//                .setRegistry(registryConfig)
                .setProtocol("bolt") // 指定协议
                .setDirectUrl("bolt://127.0.0.1:9696") // 指定直连地址
                .setConnectTimeout(10 * 1000);

        // 3. 构造服务引用
        HelloService helloService = consumerConfig.refer();

        Class test = helloService.getClass();
        // 4. 进行服务调用
        while (true){
            System.out.println(helloService.sayHello("555"));
            try{
                Thread.sleep(2000);
            }catch (InterruptedException e){
            }
        }




    }
}
